﻿using AutoMapper;
using Commonality.ReturnView;
using IService.Managemen;
using Microsoft.AspNetCore.Mvc;
using Models.Managemen;
using ModelToDto.Managemen;
using Service;
using SqlSugar;
using System.IO;
using System.Linq.Expressions;

namespace ManagementPlatform.Controllers
{
    public class RoleController : BaseController
    {

        public IMapper _Mapper { get; set; }
        public IRoleService _IRoleService { get; set; }
        public RoleController(IMapper _Mapper, IRoleService _IRoleService)
        {
            this._Mapper = _Mapper;
            this._IRoleService = _IRoleService;
        }

        /// <summary>
        /// 获取角色分页列表
        /// </summary>
        /// <param name="searchString"></param>
        /// <param name="url"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Index(string? searchString, int pageIndex = 1, int pageSize = 10)
        {
            Expressionable<Mp_Role> expressionable = new Expressionable<Mp_Role>();
            expressionable.AndIF(!string.IsNullOrWhiteSpace(searchString), c => c.Text.Equals(searchString));
            PagingData<Mp_Role> pagingData = _IRoleService.QueryPage<Mp_Role>(expressionable.ToExpression(), pageSize, pageIndex, c => c.RoleId, false);
            PagingData<RoleDTO> paging = _Mapper.Map<PagingData<Mp_Role>, PagingData<RoleDTO>>(pagingData);
            return View(paging);
        }
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="roleDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddRole([FromBody] RoleDTO roleDTO)
        {
            if (ModelState.IsValid)
            {
                var ent = _Mapper.Map<RoleDTO, Mp_Role>(roleDTO);
                var result = await _IRoleService.InsertAsync(ent);
                if (result != null)
                {
                    return Ok();
                }
                else
                {
                    return NotFound("添加失败");
                }

            }
            else
            {
                return new JsonResult(new ErrorInfo { Code = 0, Message = "模型验证错误" });
            }
        }
        /// <summary>
        /// 获取角色的配置菜单
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> RoleMenu([FromQuery] int roleId)
        {
            var userRoleList = await _IRoleService.GetRoleMenue(roleId);

            return View(userRoleList);
        }
        /// <summary>
        /// 配置角色菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> RoleMenuAdd()
        {
            var requestForm = Request.Form;
            var roleList = requestForm.Where(t => t.Value[0] == "true").Select(t => t.Key.Split('_')[1]).ToList();

            var userId = requestForm["RoleID"];
            var result = await _IRoleService.RolesMenueAdd(Convert.ToInt32(userId), roleList);
            if (result == 1)
            {
                return RedirectToAction("Index");
            }
            else
            {
                return NotFound();
            }
        }
    }
}
